package com.lilin.gps.service;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.lilin.gps.MainActivity;
import com.lilin.gps.R;
import com.lilin.gps.utils.DateUtils;

/**
 * 作者： andli
 * 时间： 2017/11/6 08:58
 * 描述： GPS后台上报服务
 */
public class GPSService extends Service implements Runnable{

    public static String tag = "andli";
    public static final int GPSNOTIFICATIONID = 10001;

    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(tag, "onCreate");

        // 设置为前台服务
        Notification notification = new Notification(R.drawable.icon, "定位服务", System.currentTimeMillis());
        //		Intent mIntent = new Intent(this, MainActivity.class);
        // PendingIntent pendIntent = PendingIntent.getActivity(this, 0, mIntent,0);
        // 取消通知点击跳转意图
        notification.setLatestEventInfo(this, "GPS定位服务", "实时获取位置信息", null);

        // 升级为前台服务
        startForeground(GPSNOTIFICATIONID, notification);

        Log.i(tag, "启动 轮播线程");

        Thread t = new Thread(this); // 主线程
        t.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(tag, "onStartCommand");

        return super.onStartCommand(intent, flags, startId);
    }

    /*** 开启子线程，实现轮询上报（锁屏后依然执行） ***/
    @Override
    public void run() {
        // 轮询执行
        while (true) {

            try {
                Thread.sleep(3 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Log.i(tag, "后台轮询服务: " + DateUtils.getCurrentTime());

            

        }
    }

    @Override
    public void onDestroy() {
        Log.i(tag, "onDestroy");
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}
